package com.ktgj.customer.controller;

import com.ktgj.common.core.keyutil.AesUtil;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.annotation.NoRepeatSubmit;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.common.HeydayUtils;
import com.ktgj.customer.domain.Insurance.heydayModel.HeydayUrlInfo;
import com.ktgj.customer.domain.Insurance.dxModel.kbdx;
import com.ktgj.sms.api.RemoteSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * 保险测试controllers
 * */
@ControllerAdvice
@RestController
@RequestMapping("/Insurance")
public class InsuranceController {

    @Autowired
    private RemoteSmsService remoteSmsService;

    @Autowired
    private ConfigurableApplicationContext configurableApplicationContext;

    private static Logger logger = LoggerFactory.getLogger(InsuranceController.class);

    @Autowired
    private HeydayUtils heydayUtils;

    @PostMapping("heyday")
    public AjaxResult getHeydayInsurance()
    {
        try
        {
            HeydayUrlInfo heydayUrlInfo = heydayUtils.heydayInsurance("train");
            return AjaxResult.success(heydayUrlInfo);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return AjaxResult.error("调用失败");
    }

    @PostMapping("/InsuranceOpen")
    public String InsPolicy() throws InvalidKeySpecException, NoSuchAlgorithmException {
        kbdx kbdx = new kbdx();
//        String info = kbdx.testAXOpen();
        String info = kbdx.SearchInsPolicy();
//        String info = kbdx.DxAccidentApply();
//        String info = kbdx.DxSurrender();
//        String info = kbdx.testAXOpen();
        return info;
    }


    @NoRepeatSubmit
    @PostMapping("/InsuranceCancel")
    public String InsuranceCancel() {
        String QRCODE_ENCRYPT_KEY = "JSJ!((*)!)!&!)!&";
        String data = URLDecoder.decode("F9GgzK99KhtObCUuiKsEZSvmgpOmAXAXkDOcvoxtiGLUs3%2fkfNxfOYt8j1jjzS%2bQWspMRzVKnjvP94IQwJwCs96CwusYVLQpUFW5VargX6s%3d");
        String deData = AesUtil.decrypt(data,QRCODE_ENCRYPT_KEY);
        return deData;
//        kbdx kbdx = new kbdx();
//        return kbdx.AXCancel();
    }

    @Log(title = "发送短信", businessType = BusinessType.OTHER, isSaveRequestData = true)
    @PostMapping("/sendMessage")
    //@SignatureValidation
    public void sendMessage(@RequestBody Map<String,String> mData)
    {
        try {
            logger.info("开始写日志");
        }catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        //直接获取nacos的管理配置
//        String startTime = configurableApplicationContext.getEnvironment().getProperty("swagger.title");
//        System.out.println(startTime);

//        Sms sms = new Sms();
//        sms.setMobile("18310769919");
//        sms.setSmsContent("我要发短信");
//        if (remoteSmsService.addSms(sms))
//        {
//            System.out.println("短信发送成功");
//        }
    }

    @PostMapping("/threadPool")
    public String threadPoolExecutor()
    {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10,
                10,
                60L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1),
                Executors.defaultThreadFactory(), (r, executors) -> {
            System.out.println("无法处理的任务：" + r.toString());
        }) {
            @Override
            protected void beforeExecute(Thread t, Runnable r) {
                System.out.println(System.currentTimeMillis() + "," + t.getName() + ",开始执行任务:" + r.toString());
            }

            @Override
            protected void afterExecute(Runnable r, Throwable t) {

                System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ",任务:" + r.toString() + "，执行完毕!");
            }

            @Override
            protected void terminated() {
                System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + "，关闭线程池!");
            }
        };
        Thread task1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("机票线程处理接口");
            }
        });
        task1.setName("机票线程");
        Thread task2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("酒店线程处理接口");
            }
        });
        task2.setName("酒店线程");
        //执行多个任务
        executor.execute(task1);
        executor.execute(task2);
        try {
            Thread.sleep(1);
            //关闭任务
            executor.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "";
    }
}
